﻿// --------------------------------------------------------------------------------------------------------------------
// <summary>
//   DeleteFilesStep.cs
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace TRlib.BizUnit.CustomSteps
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using global::BizUnit;

    /// <summary>
    /// The DeleteFilesStep deletes a file or files from a given locaton based on a filter
    /// </summary>
    /// 
    /// <remarks>
    /// The following shows an example of the Xml representation of this test step.
    /// 
    /// <code escaped="true">
    ///    <TestStep assemblyPath="" typeName="DeleteFilesStep">
    ///        <Timeout>2000</Timeout>
    ///        <Directory>C:\Recv2\</Directory>
    ///        <SearchPattern>*.xml</SearchPattern>
    ///                    
    ///    </TestStep>
    /// </code>
    ///    
    ///    <list type="table">
    ///        <listheader>
    ///            <term>Tag</term>
    ///            <description>Description</description>
    ///        </listheader>
    ///        <item>
    ///            <term>Timeout</term>
    ///            <description>Timeout to wait for the FILE to be written, in milisecs</description>
    ///        </item>
    ///        <item>
    ///            <term>Directory</term>
    ///            <description>The directory where the FILE is located</description>
    ///        </item>
    ///        <item>
    ///            <term>SearchPattern</term>
    ///            <description>The search pattern, such as "*.txt"</description>
    ///        </item>
    ///    </list>
    ///    </remarks>
    /// 
    public class DeleteFilesStep : ITestStep
    {
        #region ITestStep Members

        public void Execute(System.Xml.XmlNode testConfig, Context context)
        {
            var directory = context.ReadConfigAsString(testConfig, "Directory");
            var searchPattern = context.ReadConfigAsString(testConfig, "SearchPattern");
            var timeout = context.ReadConfigAsDouble(testConfig, "Timeout");

            context.LogInfo("Searching for files in: \"{0}{1}\"", directory, searchPattern);

            DateTime endTime = DateTime.Now + TimeSpan.FromMilliseconds(timeout);
            FileInfo[] files;

            do
            {
                DirectoryInfo di = new DirectoryInfo(directory);
                files = di.GetFiles(searchPattern);
                Thread.Sleep(100);
            } 
            while ((files.Length == 0) && (endTime > DateTime.Now));

            foreach (var file in files)
            {
                File.Delete(file.FullName);
                context.LogInfo(String.Format("Deleted file {0}", file.FullName));
            }
        }
        #endregion
    }
}
